var vm = new Vue({
    el: "#app",

    // delimiters: ['[[', ']]'],  // 声明Vue使用的模板变量语法
    data: {

        username: sessionStorage.username || localStorage.username,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        spits:null,
        comment:"",
        spit_comments:null,

        comment_tab: false,
        error_comment:false

    },
    mounted: function () {
        // 一加载就需要做的，直接是代码
        var spit_id = get_query_string("id", 1);
        // console.log(spit_id)
        axios.get(HOST + "/spits/" + spit_id)
            .then(response => {
                this.spits = response.data
                // console.log(response.data)
            });
        axios.get(HOST + "/spits/" + spit_id + "/comments/")
            .then(response => {
                this.spit_comments = response.data
                console.log(response.data)
            });
    },
    methods: {
        // 需要用到的函数，键值对 ，键是名称，值是匿名函数

        check_comment:function(){
            var comment_len = this.comment.length;
            if(comment_len<0 || comment_len>140){
                this.error_comment = true;
            }else{
                this.error_comment = false;
            }
        },

        comment_show:function(){
            // var spit_id = get_query_string("id", 1);
            this.comment_tab = true;

        },
        comment_submit:function(){
            var spit_id = get_query_string("id", 1);
            this.check_comment()
            if(this.error_comment == false){
                axios.post(HOST + "/spits/" + spit_id + "/comments/", {
                    comment: this.comment,
                    user_id:this.user_id,
                    username:this.username,
                },{
                    responseType: 'json'
                }).then(response => {
                    var return_url = get_query_string('next');
                        if (!return_url) {
                            return_url = '/spit-detail.html';
                        }
                        location.href = return_url;
                })
            }


        },

         logout: function(){
             var url= window.location.href;
             sessionStorage.clear();
             localStorage.clear();
             location.href = url;
        },
    },
    computed: {
        // 需要通过计算得到的数据，键值对 ，键是名称，值是匿名函数
    }
});